package com.xxyuan.batch._05_job_listener;

import com.xxyuan.batch._04_param_incr.DailyTimestampParamIncrementer;
import org.springframework.batch.core.*;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.listener.JobListenerFactoryBean;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.scope.context.StepContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.Map;

@SpringBootApplication
@EnableBatchProcessing
public class StatusListenerJob {
    /**
     * job调度器
     */
    @Autowired
    private JobLauncher jobLauncher;

    /***
     * job构造器工厂
     */
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    /**
     * step构造器工厂
     */
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    /**
     * 构造一个step对象执行的任务(逻辑对象)
     */
    @Bean
    public Tasklet tasklet() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                JobExecution jobExecution = stepContribution.getStepExecution().getJobExecution();
                //要执行的逻辑-step1步骤执行逻辑
                System.out.println("================================================================");
                System.out.println("作业执行中的状态:"+jobExecution.getStatus());
                System.out.println("================================================================");


                StepContext stepContext = chunkContext.getStepContext();
                StepExecution stepExecution = stepContext.getStepExecution();
                ExecutionContext stepExecutionContext = stepExecution.getExecutionContext();
                JobExecution jobExecution1 = stepExecution.getJobExecution();
                ExecutionContext jobExecutionContext = jobExecution1.getExecutionContext();
                return RepeatStatus.FINISHED;
            }
        };
    }

    /**
     * 创建一个监听器
     */
    @Bean
    public JobStateListener jobStateListener(){
        return new JobStateListener();
    }

    /**
     * 构造一个step对象
     */
    @Bean
    public Step step1() {
        //tasklet step 执行逻辑，类似 Thread()--->可以执行runable接口
        return stepBuilderFactory.get("step1").tasklet(tasklet())
                .build();
    }




    @Bean
    public Job job() {
        return jobBuilderFactory.get("job-state-listen-job1")
                .start(step1())
                .listener(JobListenerFactoryBean.getListener(new JobStateAnnoListener()))
                .build();
    }

    public static void main(String[] args) {
        /**
         * Job Instance  =  Job名称  +  识别参数
         *
         * Job Instance 一次执行创建一个 Job Execution对象
         *
         * 完整的一次Job Instance 执行可能创建一个Job Execution对象，也可能创建多个Job  Execution对象
         */
         SpringApplication.run(StatusListenerJob.class,args);
    }
}